<template>
  <tableLayout :columns="columns" :operationShow="true" :get="getData"
    :rowSelection="{ selectedRowKeys: selectedRowKeys, onSelect: onSelectItemChange, onChange: onSelectChange }" :operationMoreShow="true" ref="layout">

    <template #button>
      <!-- <a-button type="primary" style="margin-left: 20px" >彩蛋</a-button> -->
      <a-button type="primary" style="margin-left: 20px" danger @click="delDeploy" :disabled="selectedRowKeys.length !== 1">删除</a-button>
    </template>

    <template #operationMore="item">
      <a @click="StartWorkFlow(item.value.record)">启动</a>
      <a-divider type="vertical" />
    </template>

  </tableLayout>
</template>

<script lang="ts" setup>
import tableLayout from '@/components/tableLayout/tableLayout.vue';
import { getData, startWorkFlowInstance,deleteDeploy } from '@/api/flowdeploy';
import { notification, message } from 'ant-design-vue';
import { v4 as uuidv4 } from 'uuid';
import { ref } from 'vue';
//列表
const columns = [
  { title: '序号', dataIndex: 'Id' },
  { title: '名称', dataIndex: 'Name' }
]

const layout = ref();
const selectedRowKeys = ref<Array<number>>([]);
const formItem = {};
const selectItem = ref<any>({});

const onSelectChange = (selectedRowKeysValue: any) => {
  selectedRowKeys.value = selectedRowKeysValue;
}

const onSelectItemChange = (record: {}, selected: any) => {
  selectItem.value = record;
}

//@desc:  启动部署模型
const StartWorkFlow = (val: any) => {
  startWorkFlowInstance([{
    ProcessDefinitionId: val.Id,
    BusinessKey: uuidv4(),
  }]).then(e => {
    message.success("流程启动成功！");
  }).catch(err => {
    message.error(err.message || "流程启动失败!");
  });
}

//@desc: 删除部署模型
const delDeploy = () => {
  if (selectedRowKeys.value.length == 0) {
    openNotificationWithIcon('warning', '提示', '请选择要删除的部署模型!');
    return;
  }
  deleteDeploy(selectItem.value.DeploymentId).then(e => {
    if (e.data.Code == 1) {
      message.success("删除成功！")
      layout.value.getData();
      selectedRowKeys.value = [];
    } else {
      message.error(e.data.Message || "删除失败!")
    }
  }).catch(err => {
    message.error(err.message || "删除失败!")
  })
}


</script>